package com.af.system.util;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;
import java.util.Locale;

/**
 * @author : zhenyun.su
 * @comment : 日期类java.time.LocalDateTime, 实现 LocalDateTime， Long, String, Instant相互转换
 * @since : 2022/07/27, 替代DateUtils和SzyDate
 * 使用手册
 * 1转换为LocalDateTime ofDate()
 * 2转换为String ofString()
 * 3转换为Long 毫秒时间戳 Long ofTimestamp()
 * 4时间戳计算   Long ofTimestampDay|Hour()
 * 4转换为Instant 毫秒时间戳 Instant ofInstant()
 * 字符串日期格式format:
 * 参考：  DateTimeFormatter 中注释
 * 关键说明：
 * 时间戳， 从1970.1.1 00:00:00 GMT 开始统计  秒10位， 毫秒共13位前10位为秒.
 */

public final class AfDateUtils {
    public final static String UTC_SHORT = "yyyy-MM-dd'T'HH:mm:ss";
    public final static String UTC_Z = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
    public final static String UTC_G = "yyyy-MM-dd'T'HH:mm:ss+08:00";
    public final static String DEFAULT = "yyyy-MM-dd HH:mm:ss";
    public final static String DEFAULT_DATE = "yyyy-MM-dd";
    public final static String DEFAULT_TIME = "HH:mm:ss";
    public final static String LONG = "yyyy-MM-dd HH:mm:ss.SSS";
    public final static String SHORT = "yyyyMMddHHmmss";
    public final static String SHORT_DATE = "yyyyMMdd";
    public final static String SHORT_TIME = "HHmmss";

    /**
     * @comment : 1 转换为 LocalDateTime, 当前时间， 时间戳， 字符串
     */
    public static LocalDateTime ofDate() {
        return LocalDateTime.now();
    }

    public static LocalDateTime ofDate(final Long timestamp) {
        Instant instant = Instant.ofEpochMilli(timestamp);
        return LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
    }

    /*
     * @comment : 字符串作为时间值，需要考虑时区的问题
     */
    public static LocalDateTime ofDate(final String sDate) {
        return LocalDateTime.parse(sDate, DateTimeFormatter.ofPattern(DEFAULT, Locale.CHINA));
    }
    public static LocalDateTime ofDateUTC(final String sDate) {
        return LocalDateTime.parse(sDate, DateTimeFormatter.ofPattern(UTC_Z, Locale.CHINA));
    }
    public static LocalDateTime ofDate(final String sDate, final String format) {
        return LocalDateTime.parse(sDate, DateTimeFormatter.ofPattern(format, Locale.CHINA));
    }
    public static LocalDateTime ofDate(final String sDate, final String format, Locale locale) {
        return LocalDateTime.parse(sDate, DateTimeFormatter.ofPattern(format,locale));
    }
    /**
     * @comment : 2 转换为字符串
     */
    public static String ofString() {
        return ofString(LocalDateTime.now(), DEFAULT);
    }

    public static String ofString(LocalDateTime date) {
        return ofString(date, DEFAULT);
    }

    public static String ofString(String format) {
        return ofString(LocalDateTime.now(), format);
    }

    public static String ofString(LocalDateTime date, String format) {
        return date.format(DateTimeFormatter.ofPattern(format));
    }

    public static String ofString(Instant instant, String format) {
        return LocalDateTime.ofInstant(instant, ZoneId.systemDefault()).format(DateTimeFormatter.ofPattern(format));
    }

    public static String ofStringSecond(Long timestamp, String format) {
        return ofString(Instant.ofEpochSecond(timestamp), format);
    }

    public static String ofStringMilli(Long timestamp, String format) {
        return ofString(Instant.ofEpochMilli(timestamp), format);
    }


    /**
     * @comment : 3 转换为毫秒时间戳, 当前的毫秒时间戳，总共13位，前10位秒. 从1970.1.1 00:00:00 GMT 开始统计
     */
    public static Long ofTsMilli() {
        return System.currentTimeMillis();
    }
    public static Long ofTsSecond() {
        return System.currentTimeMillis()/1000;
    }
    public static Long ofTsMilli(Instant instant) {
        return instant.toEpochMilli();
    }

    public static Long ofTsMilli(LocalDateTime date) {
        return date.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }

    public static Long ofTsSecond(LocalDateTime date) {
        return date.atZone(ZoneId.systemDefault()).toInstant().getEpochSecond();
    }

    /**
     * @comment : 4 时间戳计算 获取以天为单位的时间戳，精确到毫秒，-1 表示前1天的时间抽，1 表示后1天的时间戳
     */
    public static Long ofTimestampDay(Long timestamp, int day) {
        return timestamp + day * 24 * 60 * 60 * 1000;
    }

    public static Long ofTimestampHour(Long timestamp, int hour) {
        return timestamp + hour * 60 * 60 * 1000;
    }

    public static Long ofTimestampMinute(Long timestamp, int minute) {
        return timestamp + minute * 60 * 1000;
    }

    public static Long ofTimestampSecond(Long timestamp, int second) {
        return timestamp + second * 1000;
    }

    /*
     * @comment : 5 转换为Instant
     */
    public static Instant ofInstant() {
        return Instant.now();
    }

    public static Instant ofInstant(LocalDateTime date, ZoneId zoneId) {
        return date.atZone(zoneId).toInstant();
    }

    /*
     * @comment :  java.util.Date 转换java.time.LocalDatetime
     */
    public static LocalDateTime ofConvert(Date date){
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
    }
    /*
     * @comment :  java.time.LocalDatetime转换java.util.Date
     */
    public static Date ofConvert(LocalDateTime date){
        return Date.from(date.atZone(ZoneId.systemDefault()).toInstant());
    }

    /**
     * @comment : 需屏蔽, 只为了测试
     */
//    public static void main(String[] args) {
//        System.out.println("============转换为LocalDateTime, ofDate()=================");
//        //2019-01-01 12:10:2.235 为例 1546315802235
//        System.out.println(ofDate());
//        System.out.println(ofDate(1546315802235L));
//        LocalDateTime localDateTime = ofDate("2019-01-01 12:10:02.235", "yyyy-MM-dd HH:mm:ss.SSS");
//        System.out.println(localDateTime);
//        System.out.println(localDateTime.toLocalDate());
//        System.out.println(localDateTime.toLocalTime());
//        System.out.println(localDateTime.getYear());
//        System.out.println(ofDate("2019-01-01 12:10:02", "yyyy-MM-dd HH:mm:ss"));
//
//        System.out.println("============转换为String, ofString()=================");
//        String today = ofString();
//        String today2 = ofString("yyyy/MM/dd HH:mm:ss");
//        String sdate = ofString(localDateTime, "yyyy-MM-dd HH:mm:ss");
//        System.out.println("today= " + today + "; today2= " + today + "; sdate= " + sdate);
//        System.out.println(ofStringSecond(1546315802L, "yyyy-MM-dd HH:mm:ss"));
//        System.out.println(ofStringMilli(1546315802235L, "yyyy-MM-dd HH:mm:ss.SSS"));
//
//        System.out.println("============转换为时间戳, ofTsMilli()=================");
//        System.out.println(ofTsMilli());
//        System.out.println(ofTsMilli(localDateTime));
//        System.out.println(ofTsSecond(localDateTime));
//        Long timestamp = ofTsMilli(localDateTime);
//        System.out.println(timestamp);
//        System.out.println(ofTimestampDay(timestamp, 2));
//        System.out.println(ofTimestampDay(timestamp, -2));
//        System.out.println(ofTimestampHour(timestamp, 2));
//        System.out.println(ofTimestampMinute(timestamp, 2));
//        System.out.println(ofTimestampSecond(timestamp, 2));
//     LocalDateTime pushDate = AfDateUtils.ofDateUTC("2022-07-28T08:18:37.374Z");
//     LocalDateTime pushDate1 = AfDateUtils.ofDate("2020-01-01T21:30:03+08:00", AfDateUtils.UTC_G);
//    }
}
